package conf

import (
	"gitee.com/llakcs/agile-go/log"
	"gitee.com/llakcs/aglie-layout/data/ds"
	"gorm.io/gorm/logger"
	"time"
)

type DataSourceConf struct {
	Driver string `mapstructure:"driver"`
	Source string `mapstructure:"source"`
}

func LoadDataSource(conf *DataSourceConf, zlog *log.ZapLogger) {
	l := ds.NewSqlLogger(
		zlog,
		ds.WithLogLevel(logger.Error),
		//设置超过200ms都是慢sql显示出来
		ds.WithSlowThreshold(200*time.Millisecond))
	db, err := ds.NewDBConn(
		ds.WithLogger(l),
		ds.WithDBConnUrl(conf.Source))
	if err != nil {
		panic(err)
	}
	ds.SetDS(db)
	log.Info("DataSource dsn：", conf.Source)
	log.Info("DataSource is initialized")
}
